import { get_image } from '../utils'

const Model = {
  // _name: 'res.partner',
  _description: '参与人',
  _fields: {
    name: { type: 'char', string: '名称' },
    email: { type: 'char', string: '邮箱' },
    is_company: { type: 'boolean', string: '是公司' },
    employee: { type: 'boolean', string: '是员工' },
    type: { type: 'selection', string: '地址类型' },
    category_id: {
      type: 'many2many',
      string: '标签',
      selected: true // formpage , get options 需要这个标志
      // fields: { display_name: null }
    },

    parent_id: { type: 'many2one', string: '公司' },

    user_id: { type: 'many2one', string: '销售员' },
    comment: { type: 'text', string: '备注' },
    bank_ids: {
      type: 'one2many',
      relation: 'res.partner.bank',
      string: '银行账户'

      // fields: { acc_number: null, bank_id: null }
    },

    child_ids: {
      type: 'one2many',
      relation: 'res.partner',
      string: '联系人 & 地址'
      // fields: { type: null, name: null }
    },

    customer_rank: { type: 'integer', string: '客户等级' }
  },

  metadata: {
    help: '参与人页面',

    fieldsForBrowse: {
      name: null,
      is_company: null,
      type: null,
      email: null,
      category_id: { display_name: null },
      parent_id: null,
      user_id: null,
      child_ids: { type: null, name: null, email: null },
      bank_ids: { acc_number: null, bank_id: null, acc_holder_name: null },
      comment: null
    },

    // 编辑页面 的 配置应该放在这里
    // fieldsForForm22222: {
    //   category_id: { selected: true }
    // },

    propForView: {
      fields: {
        name: null,
        is_company: null,
        type: null,
        email: null,
        user_id: null,
        category_id: {}
        // bank_ids: {
        //   propForView: {
        //     fields: {
        //       acc_number: null,
        //       bank_id: null,
        //       acc_holder_name: null
        //     }
        //   },

        //   propForList: {
        //     title: rec => {
        //       return `${rec.acc_number}`
        //     },
        //     label: rec => {
        //       return `${rec.bank_id__name}`
        //     },
        //     value: rec => {
        //       return ``
        //     }
        //   }
        // }
      },

      notebook: {
        default: 'accounting',
        pages: [
          {
            name: 'Contacts',
            label: '联系人及地址',

            fields: {
              child_ids: {
                updated: true,
                propForView: {
                  fields: {
                    type: null,
                    name: null,
                    email: null
                  }
                },

                propForList: {
                  title: rec => {
                    return `${rec.name}`
                  },
                  icon: rec => {
                    return get_image('res.partner', rec.id, 'image_1920')
                  },

                  label: rec => {
                    return ``
                  },
                  value: rec => {
                    return `${rec.type__name || ''}`
                  }
                }
              }
            }
          },
          {
            name: 'sales_purchases',
            label: '销售及采购',
            fields: { user_id: null }
          },

          {
            name: 'accounting',
            label: '开票',
            fields: {
              user_id: null,
              bank_ids: {
                updated: true,
                propForView: {
                  fields: {
                    acc_number: null,
                    bank_id: null,
                    acc_holder_name: null
                  }
                },
                propForList: {
                  title: rec => {
                    return `${rec.acc_number}`
                  },

                  label: rec => {
                    return `${rec.bank_id__name}`
                  },
                  value: rec => {
                    return ``
                  }
                }
              }
            }
          },

          {
            name: 'internal_notes',
            label: '备注',
            fields: {
              comment: { label: null }
            }
          }
        ]
      }
    },

    propForList: {
      searchs: {
        type: {
          string: '个人或公司',
          filters: {
            type_person: {
              string: '个人',
              domain: [['is_company', '=', false]]
            },
            type_company: {
              string: '公司',
              domain: [['is_company', '=', true]]
            }
          }
        },
        sale_or_purchase: {
          string: '客户或供应商',
          filters: {
            customer: { string: '客户', domain: [['customer_rank', '>', 0]] },
            supplier: { string: '供应商', domain: [['supplier_rank', '>', 0]] },
            inactive: { string: '已归档', domain: [['active', '=', false]] }
          }
        }
      },

      title: rec => {
        return `${rec.display_name}`
      },
      icon: rec => {
        return get_image('res.partner', rec.id, 'image_1920')
      },

      label: rec => {
        return `${rec.email || ''}`
      },
      value: rec => {
        return ``
      }
    }
  },

  extend: BaseClass => {
    class ModelClass extends BaseClass {
      global_domain(domain) {
        return super.global_domain(domain)
      }
      main_partner() {
        return this.call('main_partner')
      }

      find_or_create(email) {
        return this.call('find_or_create', [email])
      }

      // consultation_get(partner_id) {
      //   return this.call('consultation_get', [partner_id])
      // }

      async browse_one(rid, kwargs) {
        const res = await super.browse_one(rid, kwargs)
        return res
      }

      async search(params = {}) {
        const res = await super.search(params)
        return res
      }
    }
    return ModelClass
  }
}

const Models = {
  'res.partner': Model
}

export default Models
